import { observable, action } from 'mobx'
import * as api from 'lib/constants/api'
import { post } from 'lib/net'
import Toast from 'components/toast'
import Load from 'components/load'

// class AddressModel {
//   constructor (data) {
//     if (data) {
//     }
//   }
// }

export default class ProjectDetailVM {
  constructor (props) {
    this.props = props
    this.PageInitialization()
  }

  // 获取页面详情
  @observable projectDetail = {}

  // 当前项目Id
  @observable projectId = ''

  // 当前项目状态
  @observable projectState = ''

  // 编辑项目的数据
  @observable editProject = {}

  // 跳转到业绩流水
  @action handleToAchievement = (Id) => {
    this.props.history.push(`/achievement?${Id}`)
  }

  // 洽谈中的提交项目
  @action handleSubmitProject = async (Id) => {
    // this.props.history.push(`/achievement?${Id}`)
    console.log('洽谈中提交')
    let response = await post(api.SUBMITWZPROJECT, { Id: this.projectId })
    if (response.Success) {
      Toast.success('提交项目成功')
      setTimeout(() => {
        this.props.history.push(`/projectList`)
      }, 1000)
    } else {
      Toast.fail(response.Message)
    }
  }

  // 编辑项目
  @action handleEditProject = async () => {
    // this.props.history.push(`/addProject`)
    let response = await post(api.GETWZPROJECT, { Id: this.projectId })
    if (response.Success) {
      // this.editProject = response.Value
      // let item = JSON.stringify(response.Value)
      this.props.history.push('/addProject', {name: response.Value})
      // this.props.history.push(`/addProject?${item}`)
    } else {
      Toast.fail(response.Message)
    }
  }

  // 洽谈中、已拒绝中的删除项目
  @action handleDeleteProject = async (Id) => {
    let response = await post(api.DELETEWZPROJECT, { Id: this.projectId })
    if (response.Success) {
      Toast.success('删除项目成功')
      setTimeout(() => {
        this.props.history.push(`/projectList`)
      }, 1000)
    } else {
      Toast.fail(response.Message)
    }
  }

  @action PageInitialization = async () => {
    Load.loading(1, '加载中')
    let arrBox = this.props.history.location.search.split('?')
    this.projectId = arrBox[1]
    this.projectState = arrBox[2]
    let response = await post(api.WZPROJECTDETAIL, { Id: this.projectId })
    if (response.Success) {
      Load.loading(2, '加载完成')
      this.projectDetail = response.Value
    } else {
      Toast.fail(response.Message)
    }
  }
}
